﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager
{
    public partial class Login : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.Cookies["@UserList"] != null && Request.Cookies["@UserList"].Values != null)
            {
                cbxPass.Checked = Convert.ToBoolean(Request.Cookies["@UserList"].Values["@cbxPass"]);
                username.Text = Request.Cookies["@UserList"].Values["@username"];

                if (cbxPass.Checked)
                {
                    this.password.Attributes.Add("value", AppFunction.Decrypt(Request.Cookies["@UserList"].Values["@Password"]));
                }
            }

            Session["UserName"] = null;
            if (Request["username"] != null && Request["password"] != null)
            {
                string strusername = Request["username"].ToString();
                string strpassword = Request["password"].ToString();
                string strcbxPass = Request["cbxPass"].ToString();
                ExcuteDao usermanage = new ExcuteDao();
                DataSet dataset = usermanage.Get_LoginUser(strusername, AppFunction.Encrypt(strpassword));

                if (dataset == null || dataset.Tables.Count == 0
                    || dataset.Tables[0].Rows.Count == 0)
                {

                    Response.Write(AppFunction.ToMsgJson("用户名或密码错误，请确认！"));
                    Response.End();
                    return;
                }

                if ("0".Equals(dataset.Tables[0].Rows[0]["User_status"].ToString()))
                {
                    Response.Write(AppFunction.ToMsgJson("用户已经禁用，请联系相关负责人！"));
                    Response.End();
                    return;
                }

                SaveCookies(strusername, strpassword, strcbxPass);
                //Response.Cookies.Add(new HttpCookie("UserID", username));
                //Response.Cookies.Add(new HttpCookie("UserPassword", password));

                Session["UserName"] = dataset.Tables[0].Rows[0]["User_Name"].ToString();
                Session["UserID"] = dataset.Tables[0].Rows[0]["User_ID"].ToString();
                putUserInSession(dataset);
                Response.Write(1);
                Response.End();
            }
        }

        /// <summary>
        /// 将当前用户信息存入Session
        /// </summary>
        /// <param name="dataset">DataSet</param>
        private void putUserInSession(DataSet dataset)
        {
            UserCurrent currentUser = new UserCurrent();
            DataRow row = dataset.Tables[0].Rows[0];
            currentUser.UserID = row["User_ID"].ToString();
            currentUser.UserName = SpecialCharHandler.HtmlDiscode(row["User_Name"].ToString());
            //currentUser.UserPwd = row["User_Pwd"].ToString();
            currentUser.UserPhone = row["User_Phone"].ToString();
            currentUser.UserEmail = row["User_Email"].ToString();
            currentUser.IsEnabled = true;// "1".Equals(row["User_status"].ToString());

            Session[SessionKeys.CURRENT_USER] = currentUser;
        }

        /// <summary>
        /// 验证cookies
        /// </summary>
        /// <param name="strLetter">2位字母</param>
        /// <param name="strMd5Password">数据库中的的密码</param>
        public String GetCookies()
        {
            String strCookies = String.Empty;
            //strCookies = strLetter + GB.Common.CryptoUtility.MD5(strMd5Password);
            //strCookies = strLetter + GB.Common.CryptoUtility.MD5(strCookies);
            return strCookies;
        }
        /// <summary>
        /// 记住密码功能的加密算法
        /// </summary>
        /// <param name="strMd5Password"></param>
        public void SaveCookies(string username, string Password, string cbxPass)
        {
            Response.Cookies["@UserList"].Expires = DateTime.Now.AddDays(-1);

            Response.Cookies["@UserList"].Values.Add("@cbxPass", cbxPass);
            Response.Cookies["@UserList"].Values.Add("@username", username);
            if (!string.IsNullOrEmpty(cbxPass) && cbxPass.Equals("true"))
            {
                Response.Cookies["@UserList"].Values.Add("@Password", AppFunction.Encrypt(Password));
            }
            Response.Cookies["@UserList"].Expires = DateTime.MaxValue;
        }
    }
}
